package com.yans.views.display
{
	import flash.display.BitmapData;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import flash.utils.getTimer;
	 
	 
	public class BitmapFrameData 
	{
		private var _bd:BitmapData;
		private var _pointArr:Array;
		private var _offsetRect:Rectangle;
		public var children:Array;
		public var frames:Array;
		
		public function BitmapFrameData() {
		}
		
		public function get code():int { 
			return 0;
		}
		
		public function dispose():void {
			if(_bd) _bd.dispose();
			if(_pointArr) _pointArr.length = 0;
			_offsetRect = null;
		}
		
		public function get bitmapData():BitmapData {
			return _bd;
		}
		
		public function get pointArr():Array {
			return _pointArr;
		}
		
		public function get target():Object
		{
			// TODO Auto Generated method stub
			return null;
		}
		
		public function set target(obj:Object):void
		{
			// TODO Auto Generated method stub
			
		}
		
		
		/**
		 *	x, y : 偏移量
		 * 	width, height : 每帧最大宽高
		 */		
		public function get offsetRect():Rectangle {
			return _offsetRect;
		}
		
		/**
		 *	初始化 
		 * @param data [bitmapdata, pointArr, offsetPoint] 
		 */		
		public function init(data:Object):void {
			var dataArr:Array = data as Array;
			_bd = dataArr[0] as BitmapData;
			_pointArr = dataArr[1] as Array;
			_offsetRect = dataArr[2] as Rectangle;
		}
		
		public function compress():ByteArray {   
			var souByte:ByteArray = _bd.getPixels(_bd.rect);
			var bytes:ByteArray = new ByteArray();
			
			bytes.writeUnsignedInt(_bd.width);
			
			bytes.writeShort(_pointArr.length);
			for each(var p:Point in _pointArr) {
				bytes.writeShort(p.x);
				bytes.writeShort(p.y);
			}
			
			if(frames) {
				bytes.writeShort(frames.length);
				for each(var f:int in frames) {
					bytes.writeShort(f);
				}	
			}
			else {
				bytes.writeShort(0);
			}
			
			bytes.writeShort(_offsetRect.x);
			bytes.writeShort(_offsetRect.y);
			bytes.writeShort(_offsetRect.width);
			bytes.writeShort(_offsetRect.height);
			
			bytes.writeBytes(souByte); 
			 
//			bytes.deflate();
//			trace("#已经压缩数据", bytes.length)
			return bytes;
		}
		
		/**
		 *	解压缩二进制到Frame 
		 * @param byteArray
		 */		
		public static function byte2Frame(byteArray:ByteArray):BitmapFrameData {
			trace("#准备解压缩数据", byteArray.length)
//			byteArray.inflate();
			var width:int = byteArray.readUnsignedInt();
			var len:int = byteArray.readUnsignedShort(), i:int;
			var pointArr:Array = [], fs:Array = [];
			for(i = 0; i < len; i++) {
				pointArr.push(new Point(byteArray.readShort(), byteArray.readShort()));
			}
			len = byteArray.readUnsignedShort();
			if(fs && len) {
				fs = [];
				for(i = 0; i < len; i++) {
					fs.push(byteArray.readShort());
				}
			}
			var rect:Rectangle = new Rectangle(byteArray.readShort(), byteArray.readShort(), byteArray.readShort(), byteArray.readShort());
			var height:int = ( byteArray.bytesAvailable - 4 ) / 4 / width;
			var bmd:BitmapData = new BitmapData(width, height);
			bmd.setPixels(bmd.rect, byteArray);
			
			var frame:BitmapFrameData = new BitmapFrameData();
			frame.init([ bmd, pointArr, rect ]);
			frame.frames = fs;
			
			return frame;
			
		}
		 
		
	}
}